#include "../../lvgl/lvgl.h"
#include "ui.h"

static int kongtiao_value = 26;
static int led_value = 50;
static int chuanglian_value = 50;

int Home_flag = 0;
int Sleep_flag = 0;
int Windy_flag = 0;
int Auto_flag = 0;

int Home_modify_flag = 0;
int Sleep_modify_flag = 0;
int Windy_modify_flag = 0;

int kongtiao_modify_flag = 0;
int led_modify_flag = 0;
int chuanglian_modify_flag = 0;

static bool kongtiao_switch;
static bool led_switch;
static bool chuanglian_switch;
static bool jiashiqi_switch;

static int value_1 = 0;
static int value_2 = 0;
static int value_3 = 0;

static int tmp_x = 0 ;
static int tmp_y = 0 ;

static int last_temp = -1;

//弧形数值——标签可见
void arc_value_changed_event_cb(lv_event_t * e) {
    lv_obj_t * arc = lv_event_get_target(e);
    int value = lv_arc_get_value(arc);

    if(arc == kongtiao_arc) 
    {
        lv_label_set_text_fmt(kongtiao_label, "%d°C", value);
        kongtiao_value = value;
    }
    else 
    {
        lv_label_set_text_fmt(led_label, "%d%%", value);
        led_value = value;
    }

}

//滑杆数值——标签可见
void slider_value_changed_event_cb(lv_event_t * e) {
    lv_obj_t * slider = lv_event_get_target(e);
    int value = lv_slider_get_value(slider);

    if(slider == chuanglian_slider)
    {
        lv_label_set_text_fmt(chuanglian_label, "%d%%", value);
        chuanglian_value = value;
    }
    else if(slider == temperature)
    {
        if(value > 30 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch1, LV_STATE_CHECKED);
            lv_arc_set_value(kongtiao_arc , 24);
            lv_label_set_text(kongtiao_label, "24°C");
        }
        else if(value < 20  && Auto_flag == 1)
        {
            lv_obj_clear_state(Switch1, LV_STATE_CHECKED);
            lv_arc_set_value(kongtiao_arc , 16);
            lv_label_set_text(kongtiao_label, "16°C");
        }
        lv_label_set_text_fmt(temperature_label, "%d%%", value);
    }
    else if(slider == humidity)
    {
        if(value > 60 && Auto_flag == 1)
            lv_obj_clear_state(Switch4, LV_STATE_CHECKED);
        if(value < 30 && Auto_flag == 1)
            lv_obj_add_state(Switch4, LV_STATE_CHECKED);
        lv_label_set_text_fmt(humidity_label, "%d%%", value);
    }
    else if(slider == light)
    {
        if(value < 30 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 70);
            lv_label_set_text(led_label, "70%");
        }
        else if(value >= 30 && value < 70 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 50);
            lv_label_set_text(led_label, "50%");
        }
        else if(value >= 70 && Auto_flag == 1)
        {
            lv_obj_clear_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 0);
            lv_label_set_text(led_label, "0%");
        }
        lv_label_set_text_fmt(light_label, "%d%%", value);
    }
}

//开关的回调函数
void switch_event_cb(lv_event_t * e) {
    lv_obj_t * Switch = lv_event_get_target(e);
    bool current_state = lv_obj_has_state(Switch, LV_STATE_CHECKED);
    int value;

    if(Switch == Switch1)
    {
        if(current_state)
            lv_arc_set_value( kongtiao_arc , kongtiao_value);
        else
            lv_arc_set_value( kongtiao_arc , 16);

        value = lv_arc_get_value(kongtiao_arc);
        lv_label_set_text_fmt(kongtiao_label, "%d°C", value);
    }

    else if(Switch == Switch2)
    {
        if(current_state)
            lv_arc_set_value( led_arc , led_value);
        else
            lv_arc_set_value( led_arc , 0);

        value = lv_arc_get_value(led_arc);
        lv_label_set_text_fmt(led_label, "%d%%", value);
    }

    else if(Switch == Switch3)
    {
        if(current_state)
            lv_slider_set_value( chuanglian_slider , chuanglian_value , LV_ANIM_ON);
        else
            lv_slider_set_value( chuanglian_slider , 0 , LV_ANIM_ON);

        value = lv_slider_get_value(chuanglian_slider);                                                                            
        lv_label_set_text_fmt(chuanglian_label, "%d%%", value);
    }

}

//三种模式回调函数中通用的部分
void Switch_Fun()
{
    //空调开关
    if(kongtiao_switch)
    {
        lv_obj_add_state(Switch1, LV_STATE_CHECKED);
        lv_arc_set_value( kongtiao_arc , value_1);
        lv_label_set_text_fmt(kongtiao_label, "%d°C", value_1);
    }
    else
    {
        lv_obj_clear_state(Switch1, LV_STATE_CHECKED);
        lv_arc_set_value( kongtiao_arc , 16);
        lv_label_set_text(kongtiao_label, "16°C");
    }
    //灯光开关
    if(led_switch)
    {
        lv_obj_add_state(Switch2, LV_STATE_CHECKED);
        lv_arc_set_value( led_arc , value_2);
        lv_label_set_text_fmt(led_label, "%d%%", value_2);
    }
    else
    {
        lv_obj_clear_state(Switch2, LV_STATE_CHECKED);
        lv_arc_set_value( led_arc , 0);
        lv_label_set_text(led_label, "0%");
    }
    if(chuanglian_switch)
    {
        lv_obj_add_state(Switch3, LV_STATE_CHECKED);
        lv_slider_set_value( chuanglian_slider , value_3 , LV_ANIM_ON);
        lv_label_set_text_fmt(chuanglian_label, "%d%%", value_3);
    }
    else
    {
        lv_obj_clear_state(Switch3, LV_STATE_CHECKED);
        lv_slider_set_value( chuanglian_slider , 0 , LV_ANIM_ON);
        lv_label_set_text(chuanglian_label, "0%");
    }
    if(jiashiqi_switch)
        lv_obj_add_state(Switch4, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(Switch4, LV_STATE_CHECKED);
}

//回家模式的回调函数
void event_Home(lv_event_t * e)
{
    if(Home_flag == 0)
    {
        time_t raw_time = time(NULL);              // 获取时间戳
        struct tm *local_time = localtime(&raw_time);   // 转换为本地时间结构体
        //格式化时间字符串
        char time_str[64];
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
        //写入文件
        fprintf(fp, "[%s] 启用回家模式\n", time_str);
        //刷新缓冲区（确保立即写入磁盘）
        fflush(fp);

        //将点击的按钮背景色设置为红色，其余为默认
        lv_obj_clear_state(Sleep, LV_STATE_CHECKED);
        lv_obj_clear_state(Windy, LV_STATE_CHECKED);
        lv_obj_clear_state(Auto, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Sleep, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Windy, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Auto, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);

        lv_obj_add_state(Home, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Home, lv_color_hex(0xFF0000), LV_STATE_CHECKED);
        
        Home_flag = 1;
        Sleep_flag = 0;
        Windy_flag = 0;
        Auto_flag = 0;

        //获取page2初值设置,并打开对应的开关
        kongtiao_switch = lv_obj_has_state(home_kongtiao, LV_STATE_CHECKED);
        led_switch = lv_obj_has_state(home_led, LV_STATE_CHECKED);
        chuanglian_switch = lv_obj_has_state(home_chuanglian, LV_STATE_CHECKED);
        jiashiqi_switch = lv_obj_has_state(home_jiashiqi, LV_STATE_CHECKED);

        value_1 = home_kongtiao_value;
        value_2 = home_led_value;
        value_3 = home_chuanglian_value;

        Switch_Fun();
    }
    else
    {
        //再次点击按钮，取消当前模式
        lv_obj_clear_state(Home, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Home, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        Home_flag = 0;
    }

}

//睡觉模式的回调函数
void event_Sleep(lv_event_t * e)
{
    if(Sleep_flag == 0)
    {
        time_t raw_time = time(NULL);              // 获取时间戳
        struct tm *local_time = localtime(&raw_time); // 转换为本地时间结构体
        //格式化时间字符串
        char time_str[64];
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
        //写入文件
        fprintf(fp, "[%s] 启用睡眠模式\n", time_str);
        //刷新缓冲区（确保立即写入磁盘）
        fflush(fp);

        //将点击的按钮背景色设置为红色，其余为默认
        lv_obj_clear_state(Home, LV_STATE_CHECKED);
        lv_obj_clear_state(Windy, LV_STATE_CHECKED);
        lv_obj_clear_state(Auto, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Home, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Windy, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Auto, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);

        lv_obj_add_state(Sleep, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Sleep, lv_color_hex(0xFF0000), LV_STATE_CHECKED);

        Home_flag = 0;
        Sleep_flag = 1;
        Windy_flag = 0;
        Auto_flag = 0;

        //获取page2初值设置,并打开对应的开关
        kongtiao_switch = lv_obj_has_state(sleep_kongtiao, LV_STATE_CHECKED);
        led_switch = lv_obj_has_state(sleep_led, LV_STATE_CHECKED);
        chuanglian_switch = lv_obj_has_state(sleep_chuanglian, LV_STATE_CHECKED);
        jiashiqi_switch = lv_obj_has_state(sleep_jiashiqi, LV_STATE_CHECKED);

        value_1 = sleep_kongtiao_value;
        value_2 = sleep_led_value;
        value_3 = sleep_chuanglian_value;

        Switch_Fun();
    }
    else
    {
        //再次点击按钮，取消当前模式
        lv_obj_clear_state(Sleep, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Sleep, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        Sleep_flag = 0;
    }

}

//通风模式的回调函数
void event_Windy(lv_event_t * e)
{
    if(Windy_flag == 0)
    {
        time_t raw_time = time(NULL);              // 获取时间戳
        struct tm *local_time = localtime(&raw_time); // 转换为本地时间结构体
        //格式化时间字符串
        char time_str[64];
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
        //写入文件
        fprintf(fp, "[%s] 启用通风模式\n", time_str);
        //刷新缓冲区（确保立即写入磁盘）
        fflush(fp);

        //将点击的按钮背景色设置为红色，其余为默认
        lv_obj_clear_state(Home, LV_STATE_CHECKED);
        lv_obj_clear_state(Sleep, LV_STATE_CHECKED);
        lv_obj_clear_state(Auto, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Home, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Sleep, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Auto, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);

        lv_obj_add_state(Windy, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Windy, lv_color_hex(0xFF0000), LV_STATE_CHECKED);

        Home_flag = 0;
        Sleep_flag = 0;
        Windy_flag = 1;
        Auto_flag = 0;

        //获取page2初值设置,并打开对应的开关
        kongtiao_switch = lv_obj_has_state(windy_kongtiao, LV_STATE_CHECKED);
        led_switch = lv_obj_has_state(windy_led, LV_STATE_CHECKED);
        chuanglian_switch = lv_obj_has_state(windy_chuanglian, LV_STATE_CHECKED);
        jiashiqi_switch = lv_obj_has_state(windy_jiashiqi, LV_STATE_CHECKED);

        value_1 = windy_kongtiao_value;
        value_2 = windy_led_value;
        value_3 = windy_chuanglian_value;

        Switch_Fun();
    }

    else
    {
        //再次点击按钮，取消当前模式
        lv_obj_clear_state(Windy, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Windy, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        Windy_flag = 0;
    }

} 

//自动模式的回调函数
void event_Auto(lv_event_t * e)
{
    if(Auto_flag == 0)
    {
        time_t raw_time = time(NULL);              // 获取时间戳
        struct tm *local_time = localtime(&raw_time); // 转换为本地时间结构体
        //格式化时间字符串
        char time_str[64];
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
        //写入文件
        fprintf(fp, "[%s] 启用自动模式\n", time_str);
        //刷新缓冲区（确保立即写入磁盘）
        fflush(fp);

        //将点击的按钮背景色设置为红色，其余为默认
        lv_obj_clear_state(Home, LV_STATE_CHECKED);
        lv_obj_clear_state(Sleep, LV_STATE_CHECKED);
        lv_obj_clear_state(Windy, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Home, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Sleep, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(Windy, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);

        lv_obj_add_state(Auto, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Auto, lv_color_hex(0xFF0000), LV_STATE_CHECKED);

        Home_flag = 0;
        Sleep_flag = 0;
        Windy_flag = 0;
        Auto_flag = 1;

        lv_obj_clear_state(Switch1, LV_STATE_CHECKED);
        lv_arc_set_value( kongtiao_arc , 16);
        lv_label_set_text(kongtiao_label, "16°C");

        lv_obj_clear_state(Switch2, LV_STATE_CHECKED);
        lv_arc_set_value( led_arc , 0);
        lv_label_set_text(led_label, "0%");

        lv_obj_clear_state(Switch3, LV_STATE_CHECKED);
        lv_slider_set_value( chuanglian_slider , 0 , LV_ANIM_ON);
        lv_label_set_text(chuanglian_label, "0%");

        lv_obj_clear_state(Switch4, LV_STATE_CHECKED);
    }

    else
    {
        //再次点击按钮，取消当前模式
        lv_obj_clear_state(Auto, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(Auto, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
        Auto_flag = 0;
    }
}

//判断修改的是哪种模式，做好标志位
void event_mode_modify(lv_event_t * e)
{
    lv_obj_t * mode = lv_event_get_target(e);

    if(mode == Home_modify)
    {
        Home_modify_flag = 1;
        Sleep_modify_flag = 0;
        Windy_modify_flag = 0;
    }
    else if(mode == Sleep_modify)
    {
        Home_modify_flag = 0;
        Sleep_modify_flag = 1;
        Windy_modify_flag = 0;
    }
    else if(mode == Windy_modify)
    {
        Home_modify_flag = 0;
        Sleep_modify_flag = 0;
        Windy_modify_flag = 1;
    }

    lv_scr_load(ui_page4);
}

//点击文本框输入时，显示键盘，将文本框位于键盘上方
void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_FOCUSED) {

        lv_keyboard_set_textarea(kb, modify_value);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

        tmp_x = lv_obj_get_x( modify_value ) ;
        tmp_y = lv_obj_get_y( modify_value ) ;

        // 设置输入框悬浮于键盘的上方
        lv_obj_align_to( modify_value , kb ,  LV_ALIGN_OUT_TOP_MID , 0 , -40 );
    }

    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        lv_obj_set_x( modify_value , tmp_x );
        lv_obj_set_y( modify_value , tmp_y );
    }
}

//没有选择电器时消息框的回调函数
static void event_nochoice(lv_event_t * e)
{
    lv_obj_t* mbox= lv_event_get_current_target(e);
    uint16_t btn_id = lv_msgbox_get_active_btn(mbox);
    if(btn_id == 0)
    {
        lv_msgbox_close(mbox);
        lv_textarea_set_text(modify_value, "");
    }
}

//超出范围时消息框的回调函数
static void event_outRange(lv_event_t * e)
{
    lv_obj_t* mbox= lv_event_get_current_target(e);
    uint16_t btn_id = lv_msgbox_get_active_btn(mbox);
    if(btn_id == 0)
    {
        lv_msgbox_close(mbox);
        lv_textarea_set_text(modify_value, "");
    }
}
//修改成功时消息框的回调函数
static void event_finish(lv_event_t * e)
{
    lv_obj_t* mbox= lv_event_get_current_target(e);
    uint16_t btn_id = lv_msgbox_get_active_btn(mbox);
    if(btn_id == 0)
    {
        lv_msgbox_close(mbox);
        lv_textarea_set_text(modify_value, "");
        lv_textarea_set_placeholder_text(modify_value, "");
        kongtiao_modify_flag = 0;
        led_modify_flag = 0;
        chuanglian_modify_flag = 0;
        Home_modify_flag = 0;
        Sleep_modify_flag = 0;
        Windy_modify_flag = 0;
        lv_scr_load(ui_page2);
    }
}

//点击确定按钮时的回调函数
void finishFun(lv_event_t * e)
{
    static const char * btns[] = {"确认", ""};

    if(kongtiao_modify_flag == 0 && led_modify_flag == 0 && chuanglian_modify_flag ==0)
    {
        //弹出消息框，标题：错误，内容：请选择你要修改的电器
        lv_obj_t * mbox = lv_msgbox_create(NULL, "错误", "当前未选择任何电器，请选择要修改的电器" , btns , false);
        lv_obj_set_size(mbox , 400 , 250);
        lv_obj_add_style(mbox, &style_32, 0);
        lv_obj_center(mbox);
        lv_obj_add_event_cb(mbox, event_nochoice, LV_EVENT_VALUE_CHANGED, NULL);
        return;
    }

    int value = atoi(lv_textarea_get_text( modify_value ));
    if(value > 100 || ((value < 16 || value > 40) && kongtiao_modify_flag == 1))
    {
        //弹出消息框，标题：错误，内容：修改值超出范围，请重新填写
        lv_obj_t * mbox1 = lv_msgbox_create(NULL, "错误", "修改值超出范围，请重新填写" , btns , false);
        lv_obj_set_size(mbox1 , 400 , 250);
        lv_obj_add_style(mbox1, &style_32, 0);
        lv_obj_center(mbox1);
        lv_obj_add_event_cb(mbox1, event_outRange, LV_EVENT_VALUE_CHANGED, NULL);
        return;
    }
    if(Home_modify_flag == 1)
    {
        if(kongtiao_modify_flag == 1)
        {
            home_kongtiao_value = value;
            lv_label_set_text_fmt(home_kongtiao_txt, "初值:%d°C", home_kongtiao_value);
        }
        if(led_modify_flag == 1)
        {
            home_led_value = value;
            lv_label_set_text_fmt(home_LED_txt, "初值:%d%%", home_led_value);
        }
        if(chuanglian_modify_flag == 1)
        {
            home_chuanglian_value = value;
            lv_label_set_text_fmt(home_chuanglian_txt, "初值:%d%%", home_chuanglian_value);
        }
    }
    else if(Sleep_modify_flag == 1)
    {
        if(kongtiao_modify_flag == 1)
        {
            sleep_kongtiao_value = value;
            lv_label_set_text_fmt(sleep_kongtiao_txt, "初值:%d°C", sleep_kongtiao_value);
        }
        if(led_modify_flag == 1)
        {
            sleep_led_value = value;
            lv_label_set_text_fmt(sleep_LED_txt, "初值:%d%%", sleep_led_value);
        }
        if(chuanglian_modify_flag == 1)
        {
            sleep_chuanglian_value = value;
            lv_label_set_text_fmt(sleep_chuanglian_txt, "初值:%d%%", sleep_chuanglian_value);
        }
    }
    else if(Windy_modify_flag == 1)
    {
        if(kongtiao_modify_flag == 1)
        {
            windy_kongtiao_value = value;
            lv_label_set_text_fmt(windy_kongtiao_txt, "初值:%d°C", windy_kongtiao_value);
        }
        if(led_modify_flag == 1)
        {
            windy_led_value = value;
            lv_label_set_text_fmt(windy_LED_txt, "初值:%d%%", windy_led_value);
        }
        if(chuanglian_modify_flag == 1)
        {
            windy_chuanglian_value = value;
            lv_label_set_text_fmt(windy_chuanglian_txt, "初值:%d%%", windy_chuanglian_value);
        }
    }

    //弹出消息框，标题：错误，内容：修改值超出范围，请重新填写
    lv_obj_t * mbox1 = lv_msgbox_create(NULL, "完成", "修改成功" , btns , false);
    lv_obj_set_size(mbox1 , 400 , 200);
    lv_obj_add_style(mbox1, &style_32, 0);
    lv_obj_center(mbox1);
    lv_obj_add_event_cb(mbox1, event_finish, LV_EVENT_VALUE_CHANGED, NULL);
}

//判断哪个要修改哪个电器的值，做好标志位
void event_value_modify(lv_event_t * e)
{
    lv_obj_t * equipment = lv_event_get_target(e);
    
    if(equipment == kongtiao_modify)
    {
        kongtiao_modify_flag = 1;
        led_modify_flag = 0;
        chuanglian_modify_flag = 0;
        lv_textarea_set_placeholder_text(modify_value, "kongtiao_modify_value");
    }
    else if(equipment == led_modify)
    {
        kongtiao_modify_flag = 0;
        led_modify_flag = 1;
        chuanglian_modify_flag = 0;
        lv_textarea_set_placeholder_text(modify_value, "led_modify_value");
    }
    else if(equipment == chuanglian_modify)
    {
        kongtiao_modify_flag = 0;
        led_modify_flag = 0;
        chuanglian_modify_flag = 1;
        lv_textarea_set_placeholder_text(modify_value, "chuanglian_modify_value");
    }

}

//按键修改温度函数
int read_adc_key()
{
    //温度部分
    struct input_event ev;
    int r = read(adc_key_fd, &ev, sizeof(ev));
    if (r == -1) {
        perror("read adc key error");
        return -1;
    }
    if (ev.type != EV_KEY) {
        return -1;
    }
    if (ev.code == KEY_VOLUMEDOWN && ev.value && temperature_value < 40) 
        temperature_value += 2;
    else if (ev.code == KEY_VOLUMEUP && ev.value && temperature_value > 0) 
        temperature_value -= 2;
    
    if(last_temp !=  temperature_value)
    {
        last_temp = temperature_value;
        lv_slider_set_value(temperature , temperature_value , LV_ANIM_ON);
        lv_label_set_text_fmt(temperature_label, "%d%%", temperature_value);
        if(temperature_value > 30 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch1, LV_STATE_CHECKED);
            lv_arc_set_value(kongtiao_arc , 24);
            lv_label_set_text(kongtiao_label, "24°C");
        }
        else if(temperature_value < 20  && Auto_flag == 1)
        {
            lv_obj_clear_state(Switch1, LV_STATE_CHECKED);
            lv_arc_set_value(kongtiao_arc , 16);
            lv_label_set_text(kongtiao_label, "16°C");
        }
    }
    return 0;
}